package org.jgs1904.mybatis.mapper;

import org.jgs1904.mybatis.entity.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

/**
 * @author 墨苍璃
 * @date 2022年01月09日22时14分
 */
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {

    /**
     * sqlSession不用我们自己创建了，Spring来管理
     */
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List<User> selectAll() {

        add(new User(100, "小王11", "33333"));

//        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //方式二
        UserMapper mapper2 = getSqlSession().getMapper(UserMapper.class);
        List<User> users = mapper2.selectAll();
        delete(1);
        return users;
    }

    @Override
    public int add(User user) {
        UserMapper mapper2 = getSqlSession().getMapper(UserMapper.class);
        return mapper2.add(user);
    }

    @Override
    public int delete(int id) {
        UserMapper mapper2 = getSqlSession().getMapper(UserMapper.class);
        return mapper2.delete(id);
    }

}
